package logic

import (
	"context"
	"github.com/pkg/errors"
	"mall/app/product/model"
	"mall/common/xerr"

	"mall/app/product/cmd/rpc/internal/svc"
	"mall/app/product/cmd/rpc/pb"

	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type AddAttrLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddAttrLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddAttrLogic {
	return &AddAttrLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AddAttr 新增属性
func (l *AddAttrLogic) AddAttr(in *pb.AddAttrReq) (*pb.EmptyResp, error) {
	var m model.PmsAttr
	_ = copier.Copy(&m, in)
	_, err := l.svcCtx.AttrModel.Insert(l.ctx, &m)
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrorWithCode(xerr.DbError), "err: %v", err)
	}
	return &pb.EmptyResp{}, nil
}
